<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class LogController extends Controller
{
	public function getIndex(Request $request)
	{
		$name = $request->session()->get('username');
		$linfo = DB::table('log')->where('PersonName',$name)->get();
		return view('Lzk.Log.Log',['linfo'=>$linfo]);
	}

	public function getZhou(Request $request){
		
		// dd($linfo);	
		// 获取最近七天时间戳
		$ztime = time()-7*24*60*60;	
		$name = $request->session()->get('username');	
		$linfo = DB::select("select * from log where PersonName = '{$name}' and Date >= $ztime");	
		// dd($linfo);			
		return view('Lzk.Log.Log',['linfo'=>$linfo]);
	}

	public function getYue(Request $request)
	{
		// 获取最近一月时间戳
		$ztime = time()-30*24*60*60;	
		$name = $request->session()->get('username');	
		$linfo = DB::select("select * from log where PersonName = '{$name}' and Date >= $ztime");	
		// dd($linfo);			
		return view('Lzk.Log.Log',['linfo'=>$linfo]);
	}


	//解析添加
	public function getAdd()
	{
		return view('Lzk.Log.AddLog');
	}
	//执行添加
	public function postInsert(Request $request)
	{
		// dd($request->all());
		$ins = $request->only('Title','Content','Hidden');
		
		$ins['Date'] = time();
		$ins['PersonName'] = $request->session()->get('username');

		$res = DB::table('log')->insert($ins);
		if($res){
			return redirect('/Lzk/Log');
		}else{
			return back();
		}

	}

	//删除
	public function getDel($id)
	{
		$del = DB::table('log')->where('id',$id)->delete($id);
		if($del){
			return redirect('/Lzk/Log');
		}else{
			return back();
		}
	}

	//修改
	public function getEdit($id)
	{
		$res = DB::table('log')->where('id',$id)->first();
		// dd($res);
		return view('Lzk.Log.EditLog',['res'=>$res]);
	}

	//执行修改
	public function postUpdate(Request $request)
	{
		// dd($request->all());
		$id = $request->only('id');
		$upda = $request->only('Title','Content','Hidden');

		$res = DB::table('log')->where('id',$id)->update($upda);
		if($res){
			return redirect('/Lzk/Log');
		}else{
			return back();
		}

	}
}
